import { calendar } from '@/components/better-calendar/calendar'

function geCountDownDaytWeek(vDate,type,isLeap) {
	if(type == 1){
		var leap = false
		if(isLeap == 1){
			leap = true
		}
		console.log(vDate)
		var item = calendar.lunar2solar(vDate.split("-")[0],vDate.split("-")[1],vDate.split("-")[2],leap)
		console.log("======================")
		console.log(item)
		
		if(item!=-1){
			return item.ncWeek.replace("星期","周")
		}
		
	}else{
		var date = new Date(vDate)
		var weekDays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]
		var dayOfWeek = weekDays[date.getDay()]
		return dayOfWeek
	}
	
}

function getCountDownDayChar(days,nextTime){
	if(days == 0){
		return '';
	}else{
		if(nextTime == null){
			return '已经';
		}else{
			return '还有';
		}
	}
}


function isDateGreaterThanNow(targetDateStr, type) {

	console.log(targetDateStr + "===" + type)
	var currentDate = new Date();
	// 获取当前年份
	var currentYear = currentDate.getFullYear();
	var currentMonth = (currentDate.getMonth() + 1) >= 10 ? (currentDate.getMonth() + 1) : "0" + (currentDate
	.getMonth() + 1)
	var currentDate = currentDate.getDate() >= 10 ? currentDate.getDate() : "0" + currentDate.getDate()
	const now = new Date(currentYear + "-" + currentMonth + "-" + currentDate)

	const targetDate = new Date(targetDateStr);

	if (targetDate > now) {
		return '还有';
	} else if (targetDate < now) {
		if (type == 0) {
			return '已经';
		} else {
			return '还有';
		}

	} else {
		return '';
	}
}

function compareDayToNow(dateCompare) {
	var today = new Date();
	today.setHours(0, 0, 0, 0)
	var targetDay = new Date(dateCompare)
	targetDay.setHours(0, 0, 0, 0)
	if (today.getTime() > targetDay.getTime()) {
		return 0
	} else {
		return 1
	}
}

function compareDate(dateStart, dateEnd) {
	var dateStartTime = new Date(dateStart);
	var dateEndTime = new Date(dateEnd)
	if (dateStartTime.getTime() < dateEndTime.getTime()) {
		return 1
	} else {
		return 0
	}
}

function calculateReminderDays(targetDate) {
	const now = new Date();
	const selected = new Date(targetDate);
	const timeDiff = selected.getTime() - now.getTime();
	const daysDiff = Math.ceil(timeDiff / (1000 * 3600 * 24));
	var countdown = `${daysDiff} `;
	return countdown;
}

// 计算每周重复提醒距离指定日期的天数
function calculateWeeklyReminderDays(targetDate) {
	const now = new Date();
	const target = new Date(targetDate);
	const timeDiff = target.getTime() - now.getTime();
	let daysDiff = Math.ceil(timeDiff / (1000 * 3600 * 24)); // 到达目标日期的天数差
	const dayOfWeek = target.getDay(); // 目标日期是星期几

	if (daysDiff < 0) {
		// 如果目标日期在当前日期之前，计算下一次该重复提醒的日期
		daysDiff = 7 - Math.abs(daysDiff % 7); // 下次重复提醒的天数差
	} else if (daysDiff === 0 && now.getHours() >= target.getHours()) {
		// 如果目标日期是今天且当前时间已经超过目标时间，则计算下一次该重复提醒的日期
		daysDiff = 7;
	} else {
		// 如果目标日期在当前日期之后，计算距离下一次该重复提醒的日期还有多少天
		daysDiff = 7 - ((now.getDay() - dayOfWeek + 7) % 7);
	}

	return daysDiff;
}

// 计算每月重复提醒距离指定日期的天数
function calculateMonthlyReminderDays(targetDate) {
	const now = new Date();
	const target = new Date(targetDate);
	const currentMonth = now.getMonth();
	const targetMonth = target.getMonth();
	const currentDay = now.getDate();
	const targetDay = target.getDate();

	if (targetDay >= currentDay) {
		// 如果目标日期在当前日期之后或者是同一天，直接计算日期差
		const timeDiff = target.getTime() - now.getTime();
		return Math.ceil(timeDiff / (1000 * 3600 * 24));
	} else {
		// 如果目标日期在当前日期之前，需要计算下个月的目标日期
		const nextTarget = new Date(target.getFullYear(), currentMonth + 1, targetDay);
		const timeDiff = nextTarget.getTime() - now.getTime();
		return Math.ceil(timeDiff / (1000 * 3600 * 24));
	}
}

// 计算每年重复提醒距离指定日期的天数
function calculateYearlyReminderDays(targetDate) {
	const now = new Date();
	const target = new Date(targetDate);
	target.setFullYear(now.getFullYear()); // 将目标日期的年份设置为当前年份
	if (target.getTime() >= now.getTime()) {
		// 如果目标日期在当前日期之后或者是同一天，直接计算日期差
		const timeDiff = target.getTime() - now.getTime();
		return Math.ceil(timeDiff / (1000 * 3600 * 24));
	} else {
		// 如果目标日期在当前日期之前，需要计算下一年的目标日期
		target.setFullYear(now.getFullYear() + 1);
		const timeDiff = target.getTime() - now.getTime();
		return Math.ceil(timeDiff / (1000 * 3600 * 24));
	}
}

function calDate(vdate, type) {

	//	0'不重复提醒' 1'每周重复提醒' 2'每月重复提醒'  3'每年重复提醒'
	if (type == 0) {
		var days = calculateReminderDays(vdate)

		if (days == 0) {
			return "今"
		} else {
			return Math.abs(days)
		}

	} else if (type == 1) {
		var days = calculateWeeklyReminderDays(vdate)
		if (days == 0) {
			return "今"
		} else {
			return Math.abs(days)
		}
	} else if (type == 2) {
		var days = calculateMonthlyReminderDays(vdate)
		if (days == 0) {
			return "今"
		} else {
			return Math.abs(days)
		}
	} else if (type == 3) {
		var days = calculateYearlyReminderDays(vdate)
		if (days == 0) {
			return "今"
		} else {
			return Math.abs(days)
		}
	}
}

function getWeek(vDate) {
	var date = new Date(vDate)
	var weekDays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]
	var dayOfWeek = weekDays[date.getDay()]
	return dayOfWeek
}

function getWeekByCalendar(type) {
	var result = ""
	//MO, TU, WE, TH, FR, SA, SU
	if (type == 0) {
		result = "SU"
	} else if (type == 1) {
		result = "MO"
	} else if (type == 2) {
		result = "TU"
	} else if (type == 3) {
		result = "WE"
	} else if (type == 4) {
		result = "TH"
	} else if (type == 5) {
		result = "FR"
	} else if (type == 6) {
		result = "SA"
	}
	return result
}

function getWeekByCalendarIOS(type) {
	var result = ""
	//MO, TU, WE, TH, FR, SA, SU
	if (type == 0) {
		result = 1
	} else if (type == 1) {
		result = 2
	} else if (type == 2) {
		result = 3
	} else if (type == 3) {
		result = 4
	} else if (type == 4) {
		result = 5
	} else if (type == 5) {
		result = 6
	} else if (type == 6) {
		result = 7
	}
	return result
}

function getWeekChar(item) {
	var vDates = JSON.parse(item).repeatTime
	var list = vDates.split(",")
	var ret = "每周"

	for (var i = 0; i < list.length; i++) {
		if (i == 0) {
			if (list[i] == '1') {
				ret += "一"
			} else if (list[i] == '2') {
				ret += "二"
			} else if (list[i] == '3') {
				ret += "三"
			} else if (list[i] == '4') {
				ret += "四"
			} else if (list[i] == '5') {
				ret += "五"
			} else if (list[i] == '6') {
				ret += "六"
			} else if (list[i] == '0') {
				ret += "日"
			}
		} else {
			if (list[i] == '1') {
				ret += "、一"
			} else if (list[i] == '2') {
				ret += "、二"
			} else if (list[i] == '3') {
				ret += "、三"
			} else if (list[i] == '4') {
				ret += "、四"
			} else if (list[i] == '5') {
				ret += "、五"
			} else if (list[i] == '6') {
				ret += "、六"
			} else if (list[i] == '0') {
				ret += "、日"
			}
		}

	}
	return ret
}

function formatTimes(timestamp) {

	// 创建一个 Date 对象
	const date = new Date(timestamp);

	// 使用 Date 对象的方法获取年、月、日信息
	const year = date.getFullYear();
	const month = date.getMonth() + 1; // 月份是从 0 开始的，需要加 1
	const day = date.getDate();

	// 格式化输出日期
	const formattedDate = `${year}/${month < 10 ? '0' + month : month}/${day < 10 ? '0' + day : day}`;
	return formattedDate;
}

function getDayTarget(item) {
	var startTime = formatTimes(JSON.parse(item).startTime)
	var endTime = formatTimes(JSON.parse(item).endTime)
	var ret = startTime + "~" + endTime
	return ret
}

// {
// 	id:1,
// 	name:'不重复',
// },
// {
// 	id:2,
// 	name:'每天',
// },
// {
// 	id:3,
// 	name:'每周',
// },
// {
// 	id:4,
// 	name:'每月',
// },
// {
// 	id:5,
// 	name:'每年',
// },
// {
// 	id:6,
// 	name:'工作日',
// },
// {
// 	id:7,
// 	name:'艾宾浩斯记忆法',
// },
function getDayMatter(item) {
	var startTime = formatTimes(JSON.parse(item).startTime)
	
	var repeatId = JSON.parse(item).matterRepeat.type
	
	if (uni.getSystemInfoSync().platform == 'ios') {
		startTime =JSON.parse(item).startTime
	} 
	var str = ""
	if (repeatId == 0) {
		str = "不重复 " + startTime
	} else if (repeatId == 1) {
		str = "每天"
	} else if (repeatId == 2) {
		var repeatWeeks = JSON.parse(item).matterRepeat.repeatWeeks
		var repeatWeeksArr = repeatWeeks.split(",")
		var weeksStr = ""
		for (var i = 0; i < repeatWeeksArr.length; i++) {
			var str = ""
			if (repeatWeeksArr[i] == 1) {
				str = "日"
			} else if (repeatWeeksArr[i] == 2) {
				str = "一"
			} else if (repeatWeeksArr[i] == 3) {
				str = "二"
			} else if (repeatWeeksArr[i] == 4) {
				str = "三"
			} else if (repeatWeeksArr[i] == 5) {
				str = "四"
			} else if (repeatWeeksArr[i] == 6) {
				str = "五"
			} else if (repeatWeeksArr[i] == 7) {
				str = "六"
			}
			if (i == 0) {
				weeksStr += str
			} else {
				weeksStr += "、" + str
			}
		}
		str = "每周" + weeksStr

	} else if (repeatId == 3) {
		var repeatMonths = JSON.parse(item).matterRepeat.repeatMonths
		var repeatMonthsArr = repeatMonths.split(",")
		var monthStr = ""
		for (var i = 0; i < repeatMonthsArr.length; i++) {
			var str = ""
			str = repeatMonthsArr[i]
			if (i == 0) {
				monthStr += (str + "号")
			} else {
				monthStr += "、" + (str + "号")
			}
		}
		str = "每月" + monthStr

	} else if (repeatId == 4) {
		var repeatYear = JSON.parse(item).matterRepeat.repeatYear
		var repeatYearArr = repeatYear.split(",")
		var yearStr = ""
		for (var i = 0; i < repeatYearArr.length; i++) {
			var str = ""
			str = repeatYearArr[i]
			if (i == 0) {
				yearStr += str
			} else {
				yearStr += "、" + str
			}
		}
		str = "每年" + yearStr
	} else if (repeatId == 5) {
		str = "(工作日)"
	} else if (repeatId == 6) {
		str = "(艾宾浩斯记忆法)"
	}
	// var ret = startTime+" "+str
	var ret = str
	return ret
}

function getDayMatterByFour(item) {
	var startTime = formatTimes(item.startTime)
	
	if (uni.getSystemInfoSync().platform == 'ios') {
		startTime = item.startTime
	} 
	var repeatId = item.matterRepeat.type
	var str = ""
	if (repeatId == 0) {
		str = "不重复 " + startTime
	} else if (repeatId == 1) {
		str = "每天"
	} else if (repeatId == 2) {
		var repeatWeeks = item.matterRepeat.repeatWeeks
		var repeatWeeksArr = repeatWeeks.split(",")
		var weeksStr = ""
		for (var i = 0; i < repeatWeeksArr.length; i++) {
			var str = ""
			if (repeatWeeksArr[i] == 1) {
				str = "日"
			} else if (repeatWeeksArr[i] == 2) {
				str = "一"
			} else if (repeatWeeksArr[i] == 3) {
				str = "二"
			} else if (repeatWeeksArr[i] == 4) {
				str = "三"
			} else if (repeatWeeksArr[i] == 5) {
				str = "四"
			} else if (repeatWeeksArr[i] == 6) {
				str = "五"
			} else if (repeatWeeksArr[i] == 7) {
				str = "六"
			}
			if (i == 0) {
				weeksStr += str
			} else {
				weeksStr += "、" + str
			}
		}
		str = "每周" + weeksStr

	} else if (repeatId == 3) {
		var repeatMonths = item.matterRepeat.repeatMonths
		var repeatMonthsArr = repeatMonths.split(",")
		var monthStr = ""
		for (var i = 0; i < repeatMonthsArr.length; i++) {
			var str = ""
			str = repeatMonthsArr[i]
			if (i == 0) {
				monthStr += (str + "号")
			} else {
				monthStr += "、" + (str + "号")
			}
		}
		str = "每月" + monthStr

	} else if (repeatId == 4) {
		var repeatYear = item.matterRepeat.repeatYear
		var repeatYearArr = repeatYear.split(",")
		var yearStr = ""
		for (var i = 0; i < repeatYearArr.length; i++) {
			var str = ""
			str = repeatYearArr[i]
			if (i == 0) {
				yearStr += str
			} else {
				yearStr += "、" + str
			}
		}
		str = "每年" + yearStr
	} else if (repeatId == 5) {
		str = "(工作日)"
	} else if (repeatId == 6) {
		str = "(艾宾浩斯记忆法)"
	}
	// var ret = startTime+" "+str
	var ret = str
	return ret
}


function getDayEvent(item) {
	var startTime = formatTimes(JSON.parse(item).time)
	// if(JSON.parse(item).remark == 0){
	// 	startTime = formatTimes(JSON.parse(item).time)
	// }else{
	// 	startTime = formatTimes(JSON.parse(item).nextTime)
	// }

	var remark = JSON.parse(item).remark
	var str = ""
	if (remark == 0) {
		str = "(不重复提醒)"
	} else if (remark == 1) {
		str = "(每周重复提醒)"
	} else if (remark == 2) {
		str = "(每月重复提醒)"
	} else if (remark == 3) {
		str = "(每年重复提醒)"
	}
	var ret = startTime + " " + str
	return ret
}
//计算两个日期之间的天数
function compareDayDiff(vDate) {
	var date1 = new Date()
	date1.setHours(0, 0, 0, 0)
	var date2 = new Date(vDate)
	date2.setHours(0, 0, 0, 0)
	var timeDiff = Math.abs(date2.getTime() - date1.getTime())

	var dayDiff = Math.ceil(timeDiff / (1000 * 3600 * 24))
	return dayDiff;
}

//判断是否延期
function compareTime(startTime, endTime) {
	var today = new Date();
	today.setHours(0, 0, 0, 0)
	if (endTime != null && endTime != '') {
		var targetDay = new Date(endTime)
		targetDay.setHours(0, 0, 0, 0)
		if (today.getTime() < targetDay.getTime()) {
			return 0
		} else if (today.getTime() > targetDay.getTime()) {
			return 1
		} else {
			return 2
		}
	} else {
		var targetDay = new Date(startTime)
		targetDay.setHours(0, 0, 0, 0)
		if (today.getTime() < targetDay.getTime()) {
			return 0
		} else if (today.getTime() > targetDay.getTime()) {
			return 1
		} else {
			return 2
		}
	}
}
//格式化时间
function formatTime(startTime, endTime) {
	var today = new Date();
	today.setHours(0, 0, 0, 0)
	if (endTime != null && endTime != '') {
		var targetDay = new Date(endTime)
		targetDay.setHours(0, 0, 0, 0)
		var weekStart = getWeek(startTime)
		var weekEnd = getWeek(endTime)
		if (today.getTime() < targetDay.getTime()) {

			return startTime + "" + weekStart + "/" + endTime + "" + weekEnd
		} else if (today.getTime() > targetDay.getTime()) {

			return startTime + "" + weekStart + "/" + endTime + "" + weekEnd + " 延期" + compareDayDiff(
				endTime) + "天"
		} else {
			return startTime + "" + weekStart + "/" + endTime + "" + weekEnd + " 今天"
		}

	} else {
		var targetDay = new Date(startTime)
		targetDay.setHours(0, 0, 0, 0)
		var weekStart = getWeek(startTime)
		if (today.getTime() < targetDay.getTime()) {

			return startTime + "" + weekStart
		} else if (today.getTime() > targetDay.getTime()) {

			return startTime + "" + weekStart + " 延期" + compareDayDiff(startTime) + "天"
		} else {
			return startTime + "" + weekStart + " 今天"
		}

	}
}


function formatDateTime(date = new Date()) {
	// 确保传入的是Date对象  
	if (!(date instanceof Date)) {
		// 如果不是Date对象，则使用当前时间  
		date = new Date();
	}

	// 提取年月日时分秒  
	let year = date.getFullYear();
	let month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的  
	let day = date.getDate().toString().padStart(2, '0');
	let hours = date.getHours().toString().padStart(2, '0');
	let minutes = date.getMinutes().toString().padStart(2, '0');
	let seconds = date.getSeconds().toString().padStart(2, '0');

	// 拼接成最终的字符串  
	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
function getTenYearsLaterDateTime(inputDate,yearNum) {  
  // 确保inputDate是Date对象  
  let date = inputDate instanceof Date ? inputDate : new Date(inputDate);  
  
  // 检查inputDate是否有效  
  if (isNaN(date.getTime())) {  
    console.error('Invalid date provided');  
    return null; // 或者返回一个默认值，比如当前时间  
  }  
  
  // 获取十年后的时间  
  date.setFullYear(date.getFullYear() + yearNum);  
  
  // 提取年月日时分秒  
  let year = date.getFullYear();  
  let month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的  
  let day = date.getDate().toString().padStart(2, '0');  
  let hours = date.getHours().toString().padStart(2, '0');  
  let minutes = date.getMinutes().toString().padStart(2, '0');  
  let seconds = date.getSeconds().toString().padStart(2, '0');  
  
  // 拼接成最终的字符串  
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;  
}  


function getTenDaysLaterDateTime(inputDate,dayNum) {  
  // 确保inputDate是Date对象  
  let date = inputDate instanceof Date ? inputDate : new Date(inputDate);  
  
  // 检查inputDate是否有效  
  if (isNaN(date.getTime())) {  
    console.error('Invalid date provided');  
    return null; // 或者返回一个默认值，比如当前时间  
  }  
  
  // 获取十天后的时间  
  date.setDate(date.getDate() + dayNum);  
  
  // 提取年月日时分秒  
  let year = date.getFullYear();  
  let month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的  
  let day = date.getDate().toString().padStart(2, '0');  
  let hours = date.getHours().toString().padStart(2, '0');  
  let minutes = date.getMinutes().toString().padStart(2, '0');  
  let seconds = date.getSeconds().toString().padStart(2, '0');  
  
  // 拼接成最终的字符串  
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;  
}  

export default {
	isDateGreaterThanNow,
	calculateReminderDays,
	calculateWeeklyReminderDays,
	calculateMonthlyReminderDays,
	calculateYearlyReminderDays,
	getWeek,
	getWeekChar,
	getWeekByCalendar,
	getWeekByCalendarIOS,
	calDate,
	compareDayToNow,
	compareDate,
	compareDayDiff,
	compareTime,
	formatTime,
	getDayTarget,
	getDayMatter,
	getDayMatterByFour,
	getDayEvent,
	formatDateTime,
	getTenYearsLaterDateTime,
	getTenDaysLaterDateTime,
	getCountDownDayChar,
	geCountDownDaytWeek

}